#ifndef _EXTI_HW_H_
#define _EXTI_HW_H_

#include "soc.h"

typedef struct {
	_RW IMR;    /*!< EXTI Interrupt mask register,            offset: 0x00 */
	_RW EMR;    /*!< EXTI Event mask register,                offset: 0x04 */
	_RW RTSR;   /*!< EXTI Rising trigger selection register,  offset: 0x08 */
	_RW FTSR;   /*!< EXTI Falling trigger selection register, offset: 0x0C */
	_RW SWIER;  /*!< EXTI Software interrupt event register,  offset: 0x10 */
	_RW PR;     /*!< EXTI Pending register,                   offset: 0x14 */
} EXTI_Struct;

extern EXTI_Struct SOC_EXTI;

/**
 * EXTI0~15用于连接GPIO，PAx/PBx/../PHx/PIx共用EXTIx中断，同一时间只能选配一个,
 *  也即最多可支持PA0/PA1/../PA14/PA15等PIN号不重复的15个中断。
 * EXTI16 -- PVD输出
 * EXTI17 -- RTC闹钟事件
 * EXTI18 -- USB_OTG_FS唤醒事件
 * EXTI19 -- 以太网唤醒事件
 * EXTI20 -- USB_OTG_HS(在FS中配置)唤醒事件
 * EXTI21 -- RTC入侵和时间戳事件
 * EXTI22 -- RTC唤醒事件
 */

/**
 * 中断屏蔽
 */
#define EXTI_IMR_MASK_0_22D(n)       (0x1U<<(n))
#define EXTI_IMR_FORBID_0_22D(n)     (0x0U<<(n))
#define EXTI_IMR_PERMIT_0_22D(n)     (0x1U<<(n))

/**
 * 事件屏蔽
 */
#define EXTI_EMR_MASK_0_22D(n)       (0x1U<<(n))
#define EXTI_EMR_FORBID_0_22D(n)     (0x0U<<(n))
#define EXTI_EMR_PERMIT_0_22D(n)     (0x1U<<(n))

/**
 * 上升沿触发，外部唤醒线配置为边沿触发时，不能有毛刺
 */
#define EXTI_RTSR_MASK_0_22D(n)       (0x1U<<(n))
#define EXTI_RTSR_FORBID_0_22D(n)     (0x0U<<(n))
#define EXTI_RTSR_PERMIT_0_22D(n)     (0x1U<<(n))

/**
 * 下降沿触发，外部唤醒线配置为边沿触发时，不能有毛刺
 */
#define EXTI_FTSR_MASK_0_22D(n)       (0x1U<<(n))
#define EXTI_FTSR_FORBID_0_22D(n)     (0x0U<<(n))
#define EXTI_FTSR_PERMIT_0_22D(n)     (0x1U<<(n))

/**
 * 软件中断，通过PR寄存器清除
 */
#define EXTI_SWIER_SET_0_22D(n)       (0x1U<<(n))

/**
 * 挂起状态，读返回状态，写1清除
 */
#define EXTI_PR_CLEAR_0_22D(n)        (0x1U<<(n))
#define _EXTI_IsPend(dev, n)          ((dev).PR & (0x1U<<(n)))

#endif /* _EXTI_HW_H_ */
